package com.hassean.we.admin.controller;
import java.util.*;


import com.hassean.we.common.constant.CommonConstant;
import com.hassean.we.common.util.Query;
import com.hassean.we.common.util.R;
import com.hassean.we.model.entity.IpphoneSysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hassean.we.admin.service.IpphoneSysUserService;
import com.hassean.we.common.web.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yajun
 * @since 2019-07-01
 */
@RestController
@RequestMapping("/ipphoneSysUser")
public class IpphoneSysUserController extends BaseController {
    @Autowired private IpphoneSysUserService ipphoneSysUserService;

    /**
    * 通过ID查询
    *
    * @param params
    * @return IpphoneSysUser
    */
    @RequestMapping("/select/by/id")
    public R<IpphoneSysUser> get(@RequestBody Map<String, Object> params) {
        Integer id = Integer.parseInt(params.get("id").toString());
        return new R<>(ipphoneSysUserService.selectById(id));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public R<Map<String, Object>> page(@RequestBody Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        Map<String,Object> resultMap = new HashMap<>();
        List< Map<String, Object>> resultList = new ArrayList<>();
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        Page<IpphoneSysUser> page = ipphoneSysUserService.selectPage(new Query<>(params), new EntityWrapper<>());
        for (int i = 0; i <page.getRecords().size() ; i++) {
            Map<String,Object>map = new HashMap<>();

            map.put("identity",page.getRecords().get(i).getIdentity());
            map.put("address",page.getRecords().get(i).getAddress());
            map.put("addTime",page.getRecords().get(i).getAddTime());
            map.put("districts",page.getRecords().get(i).getDistricts());
            map.put("municipalities",page.getRecords().get(i).getMunicipalities());
            map.put("provinces",page.getRecords().get(i).getProvinces());
            map.put("userId",page.getRecords().get(i).getSysUserId());
            map.put("username",page.getRecords().get(i).getUsername());
            map.put("phone",page.getRecords().get(i).getPhone());
            map.put("townStreet",page.getRecords().get(i).getTownStreet());
            map.put("sex",page.getRecords().get(i).getSex());
            resultList.add(map);

        }


        resultMap.put("total",page.getTotal());//总条数
        resultMap.put("size",page.getSize());//每一页多少条
        resultMap.put("pages",page.getPages());//总页数
        resultMap.put("curren",page.getCurrent());//起始页
        resultMap.put("resultList",resultList);

        return new R<>(resultMap);

    }

    /**
     * 添加
     * @param  ipphoneSysUser  实体
     * @return success/false
     */
    @RequestMapping("/add")
    public  R<Object>add(@RequestBody IpphoneSysUser ipphoneSysUser) {

        R data  =    new R<>();
        Boolean flag = false;
        EntityWrapper entityWrapper =  new EntityWrapper<>();
        String username = ipphoneSysUser.getUsername().toString();
        entityWrapper.eq("username",username);
        IpphoneSysUser  sysUser =   ipphoneSysUserService.selectOne(entityWrapper);
        if (sysUser!=null){
            data.setCode(1);
            data.setMsg("用户名已存在");
            data.setData(false);

        }else{
            ipphoneSysUserService.insert(ipphoneSysUser);
            data.setCode(0);
            data.setMsg("添加成功");
            data.setData(true);


        }
        return data;
    }

    /**
     * 删除
     * @param params
     * @return success/false
     */
    @RequestMapping("/delete")
    public R<Boolean> delete( @RequestBody Map<String, Object> params) {
        Long id = Long.parseLong(params.get("id").toString());
        IpphoneSysUser ipphoneSysUser = new IpphoneSysUser();
        ipphoneSysUser.setSysUserId(id);
        ipphoneSysUser.setUpdateTime(new Date());
        ipphoneSysUser.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(ipphoneSysUserService.updateById(ipphoneSysUser));
    }

    /**
     * 编辑
     * @param  ipphoneSysUser  实体
     * @return success/false
     */
    @RequestMapping("/edit")
    public R<Boolean> edit(@RequestBody IpphoneSysUser ipphoneSysUser) {
        ipphoneSysUser.setUpdateTime(new Date());
        return new R<>(ipphoneSysUserService.updateById(ipphoneSysUser));
    }
}
